# -*- coding: utf-8 -*-
""" Auteur: David Charbonneau """
from Tkinter import *
import Client
import Server
import UseCase
import tkSimpleDialog

server = None

""" Cette classe sert à gérer l'interface graphique nécessaire à la création, la
    modification et la suppression de cas d'usages pour le programme.
    Responsabilités:
        newUseCase()
        saveUseCase()
        deleteUseCase()
        clicUseCase(event) # sélectionne un cas d'usage
        UpdateUseCase()
    Collaborateurs:
        le nom du cas d'usage
        Sa description
        L'objet client
        Les objets Tkinter associés
"""
class InterfaceUseCase:
        def __init__(self,client):
            self.master = Toplevel(client.root)
            
            self.caseName = StringVar()
            self.currentPage=0
            self.client=client
            self.listUseCase = client.server.getObjectsOfClass(UseCase.UseCase)
            Button(self.master,text="Nouveau cas d'usage",width=15,command=self.newUseCase).grid(row=0, sticky=W+N)
            Button(self.master,text="Supprimer",width=15,command=self.deleteUseCase).grid(column=2,row=5, sticky=N)               
            Button(self.master,text="Sauvegarder",width=15,command=self.saveUseCase).grid(column=2,row=4, sticky=N)
            self.listbox = Listbox(self.master,height=5)
            self.listbox.grid(columnspan=2,row=0,column=2,sticky=N)
            Label(self.master,textvariable=self.caseName).grid(row=1,column=0)#text="Cas d'usage",
            self.textUseCase = Text(self.master,width=50,height=3)     
            self.textUseCase.grid(row=2, rowspan=4, sticky=N)
            index=self.listbox.index(ACTIVE)
            for i in self.listUseCase:
                self.listbox.insert(END, i.name) 
            self.listbox.bind("<Double-Button-1>", self.clicUseCase)    
            
                    
            
        def newUseCase(self):
            useCaseName = tkSimpleDialog.askstring("Nom du cas d'usage", "Entrez le nom du cas d'usage:")
            self.listbox.insert(END,useCaseName)
            useCase= self.client.server.setObject(UseCase.UseCase(useCaseName, "" ))
            self.listUseCase.append(useCase)
            UpdateUseCase()
            
            
        def saveUseCase(self):
            index = self.listbox.index(ACTIVE)
            self.listUseCase[index].text = self.textUseCase.get(1.0,END)
            self.client.server.setObject(self.listUseCase[index])

            
            
        def deleteUseCase(self):
            index = self.listbox.index(ACTIVE)
            self.client.server.deleteObject(self.listUseCase[index])
            #self.listUseCase[index].remove()
            self.listUseCase.pop(index)
            self.listbox.delete(ACTIVE)
            #self.listUseCase = []
            #self.listUseCase = client.server.getObjectsOfClass(UseCase.UseCase)
            UpdateUseCase()

        def clicUseCase(self,event):
            index = self.listbox.index(ACTIVE)
            self.textUseCase.delete(1.0, END)
            self.textUseCase.insert(1.0, self.listUseCase[index].text)
            self.caseName.set("Cas d'usage "+self.listUseCase[index].name+ ":")            
             

            
        def UpdateUseCase(self): 
            self.listbox.delete(0, END)
            Label(self.master, text="Cas d'usage "+self.listUseCase[index].name+ ":").grid(row=1,column=0)
            for i in self.listUseCase:
                self.listbox.insert(END, i.name)    
            
            
client= None                    
    
def PlugIn(in_client):
    global client 
    client = in_client 
    b = Button(client.frame,text = "Cas d'usage",command = ShowUseCase)
    b.pack()

def ShowUseCase():
    useCase = InterfaceUseCase(client)

if __name__ == "__main__":
    root = Tk()
    interfaceUseCase = InterfaceUseCase(root)
    root.mainloop()